<?php

namespace apiend\modules\download\models;

use Yii;
use yii\db\Expression;
use yii\helpers\ArrayHelper;
use common\models\book\Book;
use common\models\shop\ShopProduct;

/**
 * 店铺商品查询模型
 * 
 * @author emhome <emhome@163.com>
 * @since 1.0
 */
class BookSearch extends Book implements DownloadExportInterface {

    use DownloadExportTrait;

    /**
     * @var string 关键字
     */
    public $keyword;

    /**
     * @var string 关键字
     */
    public $filterStatus;

    /**
     * @inheritdoc
     */
    public function rules() {
        return [
            [['keyword', 'filterStatus', 'status'], 'safe'],
        ];
    }

    /**
     * 绑定过滤条件
     * @param \common\db\ActiveQuery $query
     * @param string $alias
     */
    protected function buildQuery() {
        $query = static::find()->active();
        if ($this->keyword) {
            $query->andFilterWhere([
                'or',
                ['like', 't.company_name', $this->keyword],
                ['like', 't.contact', $this->keyword],
                ['like', 't.contact_mobile', $this->keyword],
            ]);
        }
        $orderBy = [
            't.id' => SORT_DESC,
        ];
        $query->orderBy($orderBy);
        //$query->debug();
        return $query;
    }

    /**
     * 生成下载文件的名称
     * @return string
     */
    public function exportTitle() {
        return Yii::t('message', '{name}', [
            'name' => '山东综合能源服务中心预约参观登记表',
        ]);
    }

    /**
     * 生成下载文件的名称
     * @return string
     */
    public function getTitle() {
        return Yii::t('message', '{name}', [
            'name' => '山东综合能源服务中心预约参观登记表',
        ]);
    }

    /**
     * 生成下载文件的名称
     * @return string
     */
    public function exportFileName($ext = true) {
        return Yii::t('message', '{title}_{time}{ext}', [
            'time' => date('Y-m-d_His'),
            'title' => '山东综合能源服务中心预约参观登记表',
            'ext' => $ext ? '.docx' : '',
        ]);
    }

    public function exportCaptionOptions() {
        return [];
    }

    public function getStatusText() {
        if ($this->product && $this->product->status !== ShopProduct::STATUS_ACTIVE) {
            $markLabel = $this->product->getMarkLabel('status');
        } else {
            $markLabel = $this->getMarkLabel('status');
        }
        return $markLabel['label'];
    }

    /**
     * @inheritdoc
     */
    public function exportFields() {
        $fields = [
            'product_id:excelText',
            'id:excelText',
            'product.name',
            'name',
            'categoryMaster',
            'categoryParent',
            'category.name',
            'thumb',
            'goods_no:excelText',
            'stock',
            'sales',
            'lowstock',
            'weight',
            'volume',
            'min',
            'max_credit',
            'price',
            'market_price',
            'updated_at:datetime',
            'statusText',
        ];
        if (!$this->store_id) {
            $fields[] = 'store.name';
        }
        return $fields;
    }

    /**
     * {@inheritdoc}
     */
    public function attributeLabels() {
        return ArrayHelper::merge(parent::attributeLabels(), [
            'categorysText' => '参观需求',
            'duration' => '参观时长',
            'stagesText' => '到访时间',
            'roomText' => '是否座谈',
            'waiterDepartment.name' => '公司对接部门',
        ]);
    }

    public function getCategorysText() {
        $data = parent::getCategorysText();
        return implode('、', $data);
    }

    public function getDuration() {
        return '2小时';
    }

    public function getStagesText() {
        $data = parent::getStagesText();
        return implode('、', $data);
    }

    public function getRoomText() {
        if ($this->room) {
            return $this->room->name;
        }
        return '无座谈';
    }

    public function getColumns() {
        return [
            [
                [
                    'attribute' => 'company_name',
                    'captionOptions' => [
                        'width' => 25,
                        'cellStyle' => [
                            'gridSpan' => 2
                        ]
                    ],
                    'contentOptions' => [
                        'width' => 75,
                        'cellStyle' => [
                            'gridSpan' => 3
                        ]
                    ],
                ]
            ],
            [
                [
                    'attribute' => 'categorysText',
                    'captionOptions' => [
                        'width' => 25,
                        'cellStyle' => [
                            'gridSpan' => 2
                        ]
                    ],
                ],
                'numbers'
            ],
            [
                [
                    'attribute' => 'date',
                    'captionOptions' => [
                        'width' => 25,
                        'cellStyle' => [
                            'gridSpan' => 2
                        ]
                    ],
                ],
                'duration'
            ],
            [
                [
                    'attribute' => 'stagesText',
                    'captionOptions' => [
                        'width' => 25,
                        'cellStyle' => [
                            'gridSpan' => 2
                        ]
                    ],
                ],
                'contact'
            ],
            [
                [
                    'attribute' => 'roomText',
                    'captionOptions' => [
                        'width' => 25,
                        'cellStyle' => [
                            'gridSpan' => 2
                        ]
                    ],
                ],
                'contact_mobile'
            ],
            [
                [
                    'label' => '对接部门审批',
                    'captionOptions' => [
                        'width' => 25,
                        'cellStyle' => [
                            'gridSpan' => 2
                        ]
                    ],
                    'value' => function() {
                        return '';
                    },
                ],
                'waiterDepartment.name',
            ],
            [
                [
                    'label' => '分管领导审批',
                    'captionOptions' => [
                        'width' => 25,
                        'cellStyle' => [
                            'gridSpan' => 2
                        ]
                    ],
                    'value' => function() {
                        return '';
                    },
                ],
                'waiter_name',
            ],
            [
                [
                    'label' => '公司领导审批',
                    'captionOptions' => [
                        'width' => 25,
                        'cellStyle' => [
                            'gridSpan' => 2
                        ]
                    ],
                    'value' => function() {
                        return '';
                    },
                ],
                [
                    'label' => '',
                    'value' => function() {
                        return '';
                    },
                ],
            ],
        ];
    }

}
